package com.jyb.qiyefuwu.view.fragment.usercenter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jyb.qiyefuwu.BR;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.base.MBaseFragment;
import com.jyb.qiyefuwu.databinding.FragemtFwIntellpropBinding;

import com.jyb.qiyefuwu.entity.IntellDataListItem;
import com.jyb.qiyefuwu.view.adpater.Fw_IntellpropAdapter;
import com.jyb.qiyefuwu.viewmodel.IntellpropViewModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.common.base.BaseFragment;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.jetbrains.annotations.NotNull;

public class Fw_IntellpropFragement extends AppLazyFragment {

//    @Override
//    protected Class<IntellpropViewModel> createViewModelClass() {
//        return IntellpropViewModel.class;
//    }
//
//    @Override
//    protected int initBR() {
//        return BR.viewmodel;
//    }
    private FragemtFwIntellpropBinding mBinding;
    private IntellpropViewModel mViewModel;
    private Fw_IntellpropAdapter mAdpater;


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragemt_fw_intellprop, container, false);

        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(IntellpropViewModel.class);
        mViewModel.position = getArguments().getInt("position",0);
        mBinding.setVariable(BR.viewmodel,mViewModel);
        mBinding.setLifecycleOwner(this);
        initRecyclerView();
        InitObsever();

    }

    private void initRecyclerView(){
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        mAdpater = new Fw_IntellpropAdapter(getActivity(),mViewModel.position);
        mBinding.recyclerView.setAdapter(mAdpater);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.getData(false);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getData(true);
            }
        });
        mAdpater.setOnItemClick(new AdapterOnItemClick<IntellDataListItem>() {
            @Override
            public void onItemClick(IntellDataListItem o, int position) {
                if(-1==position){//取消关注
                    mViewModel.updateFollowType(o);
                }else{

                }
            }
        });
    }


    private void InitObsever(){
        mViewModel.listdata.observe(this,listdata ->{
            if(listdata.size()<10){
                mBinding.recyclerView.setNoMore(true);
            }
            if (mViewModel.loadmore) {
                mBinding.recyclerView.loadMoreComplete();
                mAdpater.addDatas(listdata);

            } else {
                mBinding.recyclerView.refreshComplete();
                if (listdata.size() == 0) {
                    mBinding.recyclerView.setVisibility(View.GONE);
                    mBinding.errorView.setVisibility(View.VISIBLE);
                } else {
                    mBinding.errorView.setVisibility(View.GONE);
                    mBinding.recyclerView.setVisibility(View.VISIBLE);
                    mAdpater.setDatas(listdata);
                }
            }
        });

        mViewModel.mToast.observe(getViewLifecycleOwner(),message -> MToast.showLongToast(getActivity(),message));
        mViewModel.mLoading.observe(getViewLifecycleOwner(),type->{
            switch (type){
                case BaseViewModel.SHOW_LODING:
                    DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case BaseViewModel.DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    break;
                case BaseViewModel.FINISH_AND_DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    getActivity().finish();
                    break;
            }
        });
    }

    public static Fw_IntellpropFragement newInstance(int position){
        Fw_IntellpropFragement fragment = new Fw_IntellpropFragement();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    protected void onLazyLoad() {
        mViewModel.getData(false);
    }
}
